﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SafeScript.MetaModel.Statements
{
    /// <summary>
    /// Indicates that the expression given by the property "Operator" has to be treated
    /// as a binding. The operator can be an InlineFunction, FunctionOperator or an arbitrary Expression
    /// (which hosts at least one BindingTermOperator).
    /// </summary>
    public class BindingObjectOperator : Operator
    {
        public Operator Operator;
        public ITypeDef Type;

        public override int GetPrecedence()
        {
            return this.Operator.GetPrecedence();
        }

        public override ITypeDef ComputeType(ITypeDef goal, bool needsRef, ErrorReport report)
        {
            if (this.Type != null)
                return this.Type;

            if (needsRef)
            {
                this.ReportNotAReferenceType(report);
                return null;
            }
            ITypeDef t = this.Operator.ComputeType(null, false, report);
            if (t == null)
                return null;

            if ( !(t is InstanceType))
            {
                this.ReportNotAnObject(this.Node, report);
                return null;
            }
            this.Type = new InstanceType() { Type = new BindingTypeDef() { ReturnType = t } };
            return this.Type;
        }
    }
}
